rm(list = ls())

here::i_am(file.path("code", "04_board_discrimination.R"))
library(here)
source(here("code", "config.R"))

est_df <- read_parquet(here("data", "analysis", "analysis.parquet"))

res <- feols(
  is_naacp ~ 1 |
    birthyr_cards + bpl_cards + board_identifier + exemption^married_cards +
    farmer + laborer + farm_laborer |
    vet_combined ~ order_num_serial_norm,
  data = est_df,
  split = ~ qdiff_bw,
  cluster = ~ serial_num
)

p <- map_dfr(res, broom::tidy, conf.int = TRUE, .id = "sample") |>
  mutate(
    sample = as.factor(1:4)
  ) |>
  ggplot(aes(
    x = sample,
    ymin = conf.low,
    ymax = conf.high,
    y = estimate
  )) +
  geom_hline(yintercept = 0,
             linetype = "dashed",
             alpha = .7) +
  my_theme() +
  labs(y = "Effect on NAACP\nmembership (TSLS)", x = "Quartile of Black-white induction rate gap")

p_color <- p +
  geom_point(size = 2, color = "#F8766D") +
  geom_linerange(color = "#F8766D")
ggsave(file.path(fig_dir, "color", "board_discrimination.pdf"),
       plot = p_color,
       width = 5,
       height = 3)

p_bw <- p +
  geom_point(size = 2) +
  geom_linerange()
ggsave(file.path(fig_dir, "bw",  "board_discrimination.pdf"),
       plot = p_bw,
       width = 5,
       height = 3)

res2 <- feols(
  is_naacp ~ 1 |
    birthyr_cards + bpl_cards + board_identifier + exemption^married_cards +
    farmer + laborer + farm_laborer |
    vet_combined ~ order_num_serial_norm,
  data = est_df,
  split = ~ qdiff_bw_dmda,
  cluster = ~ serial_num
)

p <- map_dfr(res2, broom::tidy, conf.int = T, .id = "sample") |>
  mutate(
    quartile = as.factor(1:4)
  ) |>
  ggplot(aes(
    x = quartile,
    ymin = conf.low,
    ymax = conf.high,
    y = estimate
  )) +
  geom_hline(yintercept = 0,
             linetype = "dashed",
             alpha = .7) +
  my_theme() +
  labs(y = "Effect on NAACP\nmembership (TSLS)", x = "Quartile of adjusted Black-white induction rate gap")

p_color <- p +
  geom_point(size = 2, color = "#F8766D") +
  geom_linerange(color = "#F8766D")
ggsave(file.path(fig_dir, "color", "board_discrimination_robust.pdf"),
       plot = p_color,
       width = 5,
       height = 3)

p_bw <- p +
  geom_point(size = 2) +
  geom_linerange()
ggsave(file.path(fig_dir, "bw", "board_discrimination_robust.pdf"),
       plot = p_bw,
       width = 5,
       height = 3)
